package net.zhengxinyun.performance.App.service.cityIndex;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.App.dto.ScoreDTO;
import net.zhengxinyun.performance.App.dto.report.ReportDTO;
import net.zhengxinyun.performance.App.mapper.CityIndexMapper;
import net.zhengxinyun.performance.App.mapper.CreateReport;
import net.zhengxinyun.performance.App.service.BaseIndexService.BaseIndexService;
import net.zhengxinyun.performance.Util.DataUtil;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.CountryIndicatorsFilter;
import net.zhengxinyun.performance.filter.TownScoreFilter;
import net.zhengxinyun.performance.mapper.Indicators.CountryIndicatorsEntityMapper;
import net.zhengxinyun.performance.service.indicators.CountryIndicatorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

@Service
public class CityIndexServiceImpl extends BaseIndexService implements CityIndexService {

    @Autowired
    private CityIndexMapper cityIndexMapper;

    @Autowired
    private CountryIndicatorsEntityMapper countryIndicatorsEntityMapper;

    @Autowired
    private CreateReport createReportMapper;

    @Override
    public List<CountryIndicatorsFilter> getAllCityIndex(CountryIndicatorsFilter filter) {
        return cityIndexMapper.getAllCityIndex(filter);
    }

    // 市指标得分
    @Override
    public List<CountryIndicatorsFilter> getIndexScore(CountryIndicatorsFilter filter, UserEntity entity) {

        List<CountryIndicatorsFilter> filterList = countryIndicatorsEntityMapper.queryByTwoTable(filter);

        return filterList;
    }

    //指标分析 获取单个指标当年每个月的数据
    @Override
    public List<ScoreDTO> getYearCityIndex(Long indexId,String currentYear) {
        TreeMap<String,String> datemap = Util.yearmonth(currentYear);//当年所有月份
        CountryIndicatorsFilter filter = new CountryIndicatorsFilter();
        filter.setIndexId(indexId);
        List<CountryIndicatorsFilter> scoreList = countryIndicatorsEntityMapper.queryByTwoTable(filter);
        List<ScoreDTO> list = new ArrayList<>();
        for (Map.Entry<String,String> iter: datemap.entrySet() )
        {
            String month=iter.getValue();

            List<CountryIndicatorsFilter> collect = scoreList.stream().filter(o -> o.getDateTime().equals(month)).collect(Collectors.toList());
            if(ObjectUtils.isNotBlank(collect))
            {
                CountryIndicatorsFilter obj = collect.get(0);

                list.add( new ScoreDTO(obj.getIndexId(),
                       obj.getFinalScore(),
                       obj.getScore(),
                       0,obj.getRank(),null,null,month,null,null,obj.getProblems(),obj.getIndexProgress()

                )   );
            }else {
                list.add(new ScoreDTO(
                        new Long(0),0,0,0,0,null,null,month,null,null,"",""
                ));
            }


        }


        return list;
    }

    //保存生成的报告
    @Override
    public int saveReport(ReportDTO dto) {

        int i = createReportMapper.saveReport(dto);
        return i;
    }
    // 获取编辑后的报告
    @Override
    public List<ReportDTO> getReports(ReportDTO dto) {

        return createReportMapper.getReport(dto);
    }

    @Override
    public int delReport(Long id) {
        return createReportMapper.delreport(id);
    }


    @Override
    public int delTownReport(int indexClass,String datetime) {
        return  createReportMapper.delTownReport(indexClass,datetime);
    }

    @Override
    public int updateReport(ReportDTO dto) {
        return createReportMapper.updateReport(dto);
    }
}
